package dataBase;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

import org.dsrg.soenea.service.threadLocal.DbRegistry;

/**
 * @author @r@$H
 *
 */
public class UserRDG {
	public static final String BASE_NAME = "invites_table";
	public static final String TABLE = DbRegistry.getTablePrefix()+BASE_NAME;

	public static final String INSERT = "INSERT INTO " + TABLE + " (inviteID,userIdInviter,userIdInvited,groupID,version,status) VALUES (?,?,?,?,?,?);";
	
	public static final String UPDATE = "UPDATE " + TABLE + " AS p set p.status=?, p.version=p.version+1, p.groupID=? WHERE p.inviteID=? AND p.userIdInviter=? AND p.userIdInvited=? AND p.version=?;";
	
	public static final String DELETE = "DELETE FROM " + TABLE + " AS p WHERE p.inviteID=? AND p.version=?;";
	
	public static final String SELECT = "SELECT p.inviteID,p.userIdInviter,p.userIdInvited,p.groupID, p.version, p.status FROM " + TABLE + " AS p WHERE p.userIdInvited=?;";

	
/*	public static List<UserRDG> findAll() throws SQLException {
		List<UserRDG> result = new Vector<UserRDG>();
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(SELECT_ALL);
		ResultSet rs = ps.executeQuery();
		while(rs.next()) {
			result.add(new UserRDG(rs.getLong("p.id"), rs.getInt("p.version"), rs.getString("p.name"), rs.getInt("p.age")));
		}
		ps.close();
		return result;
	}*/
	
	public static UserRDG find(long userId) throws SQLException {
		UserRDG result = null;
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(SELECT);
		ps.setLong(1, userId);
		ResultSet rs = ps.executeQuery();
		if(rs.next()) {
			result = new UserRDG(rs.getLong("p.inviteID"),rs.getLong("p.userIdInviter"),rs.getLong("p.userIdInvited"),rs.getLong("p.groupID"), rs.getInt("p.version"), rs.getInt("p.status"));
		}
		ps.close();
		return result;
	}

	
	private long inviteID;
	private long userIdInviter;
	private long userIdInvited;
	private long groupID;
	private int version;
	private int status;
	
	public UserRDG(long inviteID, long userIdInviter,long userIdInvited,long groupID,int version,int status){
		super();
		this.inviteID=inviteID;
		this.userIdInviter = userIdInviter;
		this.userIdInvited = userIdInvited;
		this.groupID = groupID;
		this.version=version;
		this.status = status;
	}

	/**
	 * @return the userIdInviter
	 */
	public long getUserIdInviter() {
		return userIdInviter;
	}

	/**
	 * @param userIdInviter the userIdInviter to set
	 */
	public void setUserIdInviter(int userIdInviter) {
		this.userIdInviter = userIdInviter;
	}

	/**
	 * @return the userIdInvited
	 */
	public long getUserIdInvited() {
		return userIdInvited;
	}

	/**
	 * @param userIdInvited the userIdInvited to set
	 */
	public void setUserIdInvited(int userIdInvited) {
		this.userIdInvited = userIdInvited;
	}

	/**
	 * @return the groupID
	 */
	public long getGroupID() {
		return groupID;
	}

	/**
	 * @param groupID the groupID to set
	 */
	public void setGroupID(int groupID) {
		this.groupID = groupID;
	}

	/**
	 * @return the version
	 */
	public int getVersion() {
		return version;
	}

	/**
	 * @param version the version to set
	 */
	public void setVersion(int version) {
		this.version = version;
	}

	/**
	 * @return the status
	 */
	public int getStatus() {
		return status;
	}

	/**
	 * @param status the status to set
	 */
	public void setStatus(int status) {
		this.status = status;
	}



	public void insert() throws SQLException {
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(INSERT);
		ps.setLong(1, inviteID);
		ps.setLong(2, userIdInviter);
		ps.setLong(3, userIdInvited);
		ps.setLong(4, groupID);
		ps.setInt(5, version);
		ps.setInt(6, status);
		ps.executeUpdate();
		ps.close();
	}

	public int  update() throws SQLException {
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(UPDATE);
		ps.setInt(1, status);
		ps.setLong(2, groupID);
		ps.setLong(3, inviteID);
		ps.setLong(4, userIdInviter);
		ps.setLong(5, userIdInvited);
		ps.setInt(6, version);		
		int count = ps.executeUpdate();
		ps.close();
		return count;
	}

	public int  delete() throws SQLException {
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(DELETE);
		ps.setLong(1, inviteID);
		ps.setInt(2, version);
		int count = ps.executeUpdate();
		ps.close();
		return count;
	}

}
